package com.kzx.blog.service.impl;

import com.kzx.blog.config.model.FailedException;
import com.kzx.blog.config.model.FailedInfo;
import com.kzx.blog.mapper.BlogMapper;
import com.kzx.blog.mapper.CommentMapper;
import com.kzx.blog.model.Comment;
import com.kzx.blog.model.Token;
import com.kzx.blog.service.IComment;
import com.kzx.blog.service.model.CommentAddParam;
import com.kzx.blog.service.model.CommentPagingParam;
import com.kzx.blog.util.DateUtils;
import com.kzx.blog.util.PageUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author kongz
 * @version 1.0 2021/12/14 10:07
 * @description 评论service接口实现类
 */
@Service
public class CommentImpl implements IComment {

    private final BlogMapper blogMapper;

    private final CommentMapper commentMapper;

    public CommentImpl(BlogMapper blogMapper, CommentMapper commentMapper) {
        this.blogMapper = blogMapper;
        this.commentMapper = commentMapper;
    }

    @Override
    public int add(CommentAddParam commentAddParam) {

        if (blogMapper.getById(commentAddParam.getBlogId()) == null) {// 查询博客是否存在
            throw new FailedException(FailedInfo.EMPTY_BLOG.getErrorCode(), FailedInfo.EMPTY_TOKEN.getErrorMsg());
        }

        Comment comment = new Comment();
        comment.setBlogId(commentAddParam.getBlogId());
        comment.setUserName(commentAddParam.getUserName());
        comment.setUserEmail(commentAddParam.getUserEmail());
        comment.setCreatedTime(DateUtils.getTime());
        comment.setContent(commentAddParam.getContent());
        comment.setStatus(Comment.STATUS.NOT_APPROVED);

        try {
            return commentMapper.add(comment);
        } catch (Exception e) {
            e.printStackTrace();
            throw new FailedException(FailedInfo.ERROR_INSERT.getErrorCode(), FailedInfo.ERROR_INSERT.getErrorMsg());
        }
    }

    @Override
    public PageUtils<Comment> paging(CommentPagingParam commentPagingParam, int uid) {
        Map<String, Object> map = PageUtils.getPagingMap(commentPagingParam);
        if (commentPagingParam.getBlogId() > 0) {
            map.put("blogId", commentPagingParam.getBlogId());
        } else {
            map.put("uid", uid);
        }
        if (commentPagingParam.getStatus() > -1) {
            map.put("status", commentPagingParam.getStatus());
        }

        return new PageUtils<>(commentPagingParam.getPageIndex(), commentPagingParam.getPageSize(), commentMapper.getCount(map), commentMapper.get(map));
    }

    @Override
    public int pass(int id, int status, Token token) {

        Comment comment = commentMapper.getById(id);
        // 判断是否为本人操作
        if (blogMapper.getById(comment.getBlogId()).getAuthor() != token.getUid()) {
            throw new FailedException(FailedInfo.NO_PERMISSION.getErrorCode(), FailedInfo.NO_PERMISSION.getErrorMsg());
        }

        if (status == Comment.STATUS.PASSED) {
            comment.setStatus(Comment.STATUS.PASSED);
        } else if (status == Comment.STATUS.NOT_PASSED) {
            comment.setStatus(Comment.STATUS.NOT_PASSED);
        } else {
            throw new FailedException(FailedInfo.ILLEGAL_PARAM.getErrorCode(), FailedInfo.ILLEGAL_PARAM.getErrorMsg());
        }

        try {
            return commentMapper.update(comment);
        } catch (Exception e) {
            e.printStackTrace();
            throw new FailedException(FailedInfo.ERROR_UPDATE.getErrorCode(), FailedInfo.ERROR_UPDATE.getErrorMsg());
        }
    }

    @Override
    public int delete(int id, Token token) {
        Comment comment = commentMapper.getById(id);
        // 判断是否为本人操作
        if (blogMapper.getById(comment.getBlogId()).getAuthor() != token.getUid()) {
            throw new FailedException(FailedInfo.NO_PERMISSION.getErrorCode(), FailedInfo.NO_PERMISSION.getErrorMsg());
        }

        comment.setStatus(Comment.STATUS.DELETED);

        try {
            return commentMapper.update(comment);
        } catch (Exception e) {
            e.printStackTrace();
            throw new FailedException(FailedInfo.ERROR_UPDATE.getErrorCode(), FailedInfo.ERROR_UPDATE.getErrorMsg());
        }
    }
}
